<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="crud.queryInput.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="crud.queryInput.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="项目">
            <!-- 手动设置 Select -->
            <project-select v-model="form.projectId" />
          </el-form-item>
          <el-form-item label="员工">
            <!-- 选择组件 -->
            <person-select :value="[form.staffBase.centerBase.departmentId.id,form.staffBase.centerBase.id,form.staffId]" @input="personSelectInput" />
          </el-form-item>
          <el-form-item label="工作类型">
            <el-input v-model="form.workType" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="工作职位">
            <el-input v-model="form.workPosition" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="工作内容">
            <el-input v-model="form.workContent" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="开始时间">
            <el-date-picker v-model="form.startTime" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="结束时间">
            <el-date-picker v-model="form.endTime" type="datetime" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('id')" prop="id" label="ID" />
        <el-table-column v-if="columns.visible('projectId')" prop="projectBase.projectName" label="项目" />
        <el-table-column v-if="columns.visible('staffId')" prop="staffBase.empName" label="员工" />
        <el-table-column v-if="columns.visible('workType')" prop="workType" label="工作类型" />
        <el-table-column v-if="columns.visible('workPosition')" prop="workPosition" label="工作职位" />
        <el-table-column v-if="columns.visible('workContent')" prop="workContent" label="工作内容" />
        <el-table-column v-if="columns.visible('startTime')" prop="startTime" label="开始时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('endTime')" prop="endTime" label="结束时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','projectStaff:edit','projectStaff:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudProjectStaff from '@/api/projectStaff'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import projectSelect from '@/views/components/cascader/project_cascader'
import personSelect from '@/views/components/cascader/person_cascader'

// crud交由presenter持有
const defaultCrud = CRUD({ queryOnPresenterCreated: false, title: '项目人员', url: 'api/projectStaff', sort: 'id,desc', crudMethod: { ...crudProjectStaff }})
const defaultForm = {
  id: null,
  projectId: null,
  staffId: null,
  staffBase: { centerBase: { id: null, departmentId: { id: null }}},
  workType: null,
  workPosition: null,
  workContent: null,
  startTime: null,
  endTime: null
}
export default {
  name: 'ProjectStaff',
  components: { pagination, crudOperation, rrOperation, udOperation, projectSelect, personSelect },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      permission: {
        add: ['admin', 'projectStaff:add'],
        edit: ['admin', 'projectStaff:edit'],
        del: ['admin', 'projectStaff:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'projectId', display_name: '项目ID' },
        { key: 'projectName', display_name: '项目名称' },
        { key: 'staffId', display_name: '员工ID' },
        { key: 'staffName', display_name: '员工' },
        { key: 'workType', display_name: '工作类型' },
        { key: 'workPosition', display_name: '工作职位' },
        { key: 'workContent', display_name: '工作内容' }
      ],
      pageCount: 0
    }
  },
  created() {
    // 初始化搜索条件
    this.crud.query.type = null
    this.crud.queryInput.value = null
    this.crud.query = {}
    // 设置默认搜索条件
    if (this.$route.query.mode === 'ONE') {
      this.crud.queryInput.type = 'projectId'
      this.crud.queryInput.value = this.$route.query.projectId
    }
    // 注意这里需要手动refreash加载数据，如果crud自动加载数据，则无法保证设置query发生在refreah之前
    // this.crud.refresh()
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      if (this.pageCount === 0) {
        if (this.$route.query.mode === 'ONE') {
          this.crud.queryInput.type = 'projectId'
          this.crud.queryInput.value = this.$route.query.projectId
        }
        this.pageCount = this.pageCount + 1
      }
      // 叠加queryInput中的搜索条件
      if (this.crud.queryInput.type && this.crud.queryInput.value) {
        this.crud.query[this.queryInput.type] = this.queryInput.value
      }
      return true
    },
    personSelectInput(e) {
      this.form.staffId = e[2]
      this.form.staffBase.centerBase.id = e[1]
      this.form.staffBase.centerBase.departmentId.id = e[0]
    },
    beforeCrudSubmitCU() {
      // 提交前处理this.crud.form数据
      const f = JSON.parse(JSON.stringify(this.form))
      f.staffBase = null
      this.crud.form = f
    },
    afterCrudSubmitCU() {
      // 提交后还原
      this.crud.form = this.form
    }
  }
}
</script>

<style scoped>

</style>
